import Vue from "vue";
import Vuex from "vuex";
import * as api from "../api";
import allRoutes from '@/router/allRoutes';//所有的路由配置
import dynamicRoutes from '@/router/dynamicRoutes';//动态路由
import recursionRoutes from '@/utils/recursionRoutes';//计算路由的方法
import router from "../router";

Vue.use(Vuex);
let userInfo = localStorage.getItem("qf-userInfo")||"{}";
let currentChargeProduct = localStorage.getItem("qf-currentChargeProduct");
export default new Vuex.Store({
  state: {
    userInfo:JSON.parse(userInfo),//用户信息
    sideMenu:[],//侧边栏菜单
    breadCrumb:[],//面包屑数据
    dialogVisible:false,//控制dialog显示
    dialogTitle:"增加学员信息",//dialog标题
    stuData:[],
    loading:true,//学员表格加载动画
    currentRow:{},//编辑的时候，dialog回显数据
    stuTotal:0,//表格数据总条目
    stuPageSize:10,//每页显示的数据数量
    orderData:[],//订单
    currentChargeProduct:JSON.parse(currentChargeProduct),//当前充值的产品
    order_id:"",//订单id
  },
  getters: {},
  mutations: {
    //保存用户信息
    SET_USER_INFO(state,payload){
      state.userInfo = payload
      
    },
    SET_MENULIST(state,payload){
      state.sideMenu = payload;
      //console.log(state.sideMenu)
      //将用户路由动态的添加到router的配置routes中
      //将用户路由添加到dynamicRoutes中的"/"路径那个配置中的children属性中
      let target = dynamicRoutes.find(item => item.path === "/");
      // console.log(target)      
      
      target.children = [...payload,...target.children]
      //要将用户路由动态的添加到我们的项目之中
      // console.log(dynamicRoutes)
      router.addRoutes(dynamicRoutes)
      //核心方法就是router.addRoutes([])
      // 预支付不需要展示在侧边菜单栏
      // 要排除不需要展示在菜单栏的页面 pre_order payment
      let excludeRoutes = ["pre_order","payment"]
      target.children = target.children.filter(item=>!excludeRoutes.includes(item.path))
      state.sideMenu = target.children

    },
    SET_BREADCRUMB(state,payload){
      state.breadCrumb = payload

    },
    // 显示dialog
    SHOW_DIALOG(state){
      state.dialogVisible = true
    },
    // 隐藏dialog
    HIDE_DIALOG(state){
      state.dialogVisible = false
    },
    // 修改dialog标题
    CHANGE_DIALOG_TITLE(state,payload){
      state.dialogTitle = payload
    },
    SET_CURRENT_ROW(state,payload){
      state.currentRow = {...payload}
    },
    // 设置学员表格数据
    SET_STU_DATA(state,payload){
      state.stuData = payload.data;//设置学员列表
      state.loading = false //关闭表格加载动画
      state.stuTotal = payload.total //设置学员分页总数
    },
    //显示学员表格加载动画
    SHOW_LOADING(state){
      state.loading = true

    },
    // 隐藏学员表格加载动画
    HIDE_LOADING(state){
      state.loading = false

    },
    // 设置订单数据
    SET_ORDER_DATA(state,payload){
      state.orderData = payload
    },
    //设置当前充值的产品
    SET_CURRENT_CHARGE_PRODUCT(state,payload){
      state.currentChargeProduct = [payload]
      //pre_order页面刷新数据会丢失=>存一份到localStorage
      localStorage.setItem("qf-currentChargeProduct",JSON.stringify([payload]))
    },
    //设置订单id
    SET_ORDER_ID(state,payload){
      state.order_id = payload
    }


  },
  actions: {
    async GET_MENULIST({commit}){ //用于获取服务器中当前用户菜单
      //console.log('action执行了')
      //用于获取服务器中当前用户的菜单
      let res = await api.getMenulistApi();
      let { menuList } = res.data;
      //使用recursionRoutes方法,计算出用户的路由,提交给mutation
      let userRoutes = recursionRoutes(menuList, allRoutes)
      commit("SET_MENULIST", userRoutes)

    },
    GET_STULIST({commit},payload={}){ //获取学员列表
      commit("SHOW_LOADING") //打开表格加载动画
      let {page=1,count=10,classes=""} = payload; //解构传递的参数
      api.getStuListApi(page,count,classes)
      .then(res=>{
        commit("SET_STU_DATA",res.data)
      })
    },
    // 获取订单数据
    GET_ORDER_DATA({commit},payload){
      let order_id = payload || ""
      api.getOrderApi(order_id)
      .then(res=>{
        console.log(res.data);
        if(res.data.state){
          console.log(res.data.data);
          commit("SET_ORDER_DATA",res.data.data)
        }else{
          this.$message.error("获取订单失败")
        }
      })

    }
  },
  modules: {},
});
